<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <title>编辑样式</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/bower_components/Ionicons/css/ionicons.min.css">
  <link rel="stylesheet" href="/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!--头部-->
  <header id="headerDiv" class="main-header">
    <!--#include  file="/pages/admin/header.html"-->
  </header>

  <!--左侧边栏 -->
  <aside id="sidebarDiv" class="main-sidebar">
    <!--#include  file="/pages/admin/sidebar.html"-->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <small>编辑样式</small>
      </h1>
      <ol class="breadcrumb">
        <li><i class="fa fa-paste"></i> 模板管理</li>
        <li><a href="javascript:goBack();">样式列表</a></li>
        <li>编辑样式</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box box-primary">

            <!-- form start -->
            <form id="dataForm" class="form-horizontal">
              <div class="box-body">
                <div class="form-group">
                  <label for="name" class="col-sm-2 control-label">适用于广告样式</label>
                  <div class="col-sm-5">
                    <input type="hidden" name="id" id="id"  class="form-control">
                    <select id="type_id" name="type_id" class="form-control">
                      <option value="1">样式A</option>
                      <option value="3">样式B</option>
                      <option value="3">样式C</option>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="name" class="col-sm-2 control-label">样式名称</label>
                  <div class="col-sm-5">
                    <input type="text" name="name" class="form-control" id="name">
                  </div>
                </div>
                <div class="form-group">
                  <label for="count" class="col-sm-2 control-label">广告数量</label>
                  <div class="col-sm-2">
                    <input type="number" name="count" maxlength="20" class="form-control" id="count">
                  </div>
                </div>
                <div class="form-group">
                  <label for="sizes" class="col-sm-2 control-label">绑定尺寸</label>
                  <div class="col-sm-3" id="sizes">
                  </div>
                </div>
                <div class="form-group">
                  <label for="viewjs" class="col-sm-2 control-label">viewjs</label>
                  <div class="col-sm-5">
                    <textarea id="viewjs" name="viewjs" class="form-control" rows="10"></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label for="iframejs" class="col-sm-2 control-label">iframejs</label>
                  <div class="col-sm-5">
                    <textarea  id="iframejs" name="iframejs" class="form-control" rows="10"></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label for="intro" class="col-sm-2 control-label">说明</label>
                  <div class="col-sm-5">
                    <input  name="intro" class="form-control" id="intro"/>
                  </div>
                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                  <button type="button" onclick="goBack();" class="btn btn-default">取消</button>
                  <button type="button" onclick="submitForm()" class="btn btn-primary ">提交</button>
                </div>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>
        </div>
      </div>
    </section>
  </div>
  <!--底部-->
  <footer id="footerDiv" class="main-footer">
    <!--#include  file="/pages/admin/footer.html"-->
  </footer>

  <!-- Control Sidebar -->
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/morris.js/morris.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script src="/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/bower_components/fastclick/lib/fastclick.js"></script>
<script src="/dist/js/adminlte.min.js"></script>

<script src="/dist/js/layer/layer.js"></script>
<script src="/dist/js/bootstrapValidator.min.js"></script>
<script src="/dist/js/store.min.js"></script>

<script>
    var flag=true;
    $(function () {
        init();
        //表单验证
        $('#dataForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '样式名称不能为空'
                        }
                    }
                }
                /*gameType: {
                    validators: {
                        notEmpty: {
                            message: '游戏类型不能为空'
                        },
                        stringLength: {
                            max: 2,
                            message: '类型不能超过2个字符'
                        }
                    }
                }*/
                /*stock: {
                    validators: {
                        notEmpty: {
                            message: '库存数量不能为空'
                        },
                        regexp: {
                            regexp: /^(-|\+)?\d+$/,
                            message: '库存数量请输入整数值'
                        }
                    }
                }*/

            }
        });


    });

    function init() {
        let styleid = store.get('styleid');
        //加载初始化数据
        let url="/lmgame/style/getOne";
        $.ajax({
            type : 'POST',
            url : url,
            data :{'id':styleid},
            dataType : "json",
            success: function(data) {
                let thtml = "";
                let types = data.data.types;
                $.each(types, function (i, v) {
                    thtml += "<option value='";
                    thtml += v.id
                    thtml += "'>";
                    thtml += v.name;
                    thtml += "<//option>";
                });
                $("#type_id").html(thtml);
                let shtml = "";
                let sizes = data.data.sizes;
                $.each(sizes, function (i, v) {
                    let value = v.width+"*"+v.height;
                    shtml += "<label class='radio-inline'/>" + value + "<input id='"+value+"' value='"+value+"' type='checkbox' name='sizes'>";
                });
                $("#sizes").html(shtml);
                if(data.data.style != null){
                    $("#name").val(data.data.style.name);
                    $("#count").val(data.data.style.count);
                    $("#viewjs").val(data.data.style.viewjs);
                    $("#iframejs").val(data.data.style.iframejs);
                    $("#intro").val(data.data.style.intro);
                    $("#id").val(data.data.style.id);
                    let sizes = data.data.style.sizes.split(",");
                    $.each(sizes, function (i, v) {
                        $("input:checkbox[name='sizes'][value='"+v+"']").prop({checked:true});
                    });
                }
            },
            error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    function submitForm() {
        $('#dataForm').bootstrapValidator('validate');

        if($("#dataForm").data("bootstrapValidator").isValid()){
            let url="/lmgame/style/add";
            let id = $("#id").val();
            if(id !== ''){
                url="/lmgame/style/update";
            }
            $.ajax({
                type : 'POST',
                url : url,
                data :$("#dataForm").serialize(),
                dataType : "json",
                success: function(data) {
                    if(data.code == 0){
                        window.location.href = "styleList.html";
                        layer.msg('保存成功', {
                            icon: 1,
                            time: 500 //3秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            flag=true;
                        });
                    }else{
                        layer.msg(data.msg);
                    }
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if (xhr.status === 400) {
                        window.location.href = "/pages/admin/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        }
    }


    function  goBack() {
        window.history.back(-1);
    }
</script>
</body>
</html>
